#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include"sqlist.h"




	void sqlist_create(sqlist **p)
{
	*p = malloc(sizeof(**p));
	if(*p == NULL)
		exit(1);
	(*p)->last = -1;
}
	
	int sqlist_insert(sqlist *me,int i,datatype *data)
{
	int j;
	if(me->last == DATASIZE-1)
	{
		return -1;
	}
	if(i < 0 && i > me->last+1)
	{
		return -2;
	}
	j = (me->last);
	for(;i <= j;j--)
		me->data[j+1] = me->data[j];
	me->data[i] = *data;
	me->last++;
	return 0;
}

	int sqlist_delete(sqlist *me,int i)
{
	int j;
	if(i < 0 && i > me->last+1)
	{
		printf("$$$$");
		return -1;
	}
	for(j = i;j < (me->last);j++)
		me->data[j] = me->data[j+1];
	me->last--;
	return 0;
}

	int sqlist_find(sqlist *me,datatype *data)
{
	int i;
	if(sqlist_isempty(me) == 0)
		return -1;
	for(i = 0;i <= me->last;i++)
		if(me->data[i] == *data)
		return 0;
		return -2;
}	

	int sqlist_isempty(sqlist *me)
{
	if(me->last == -1)
		return 0;
	return -1;
}

	int sqlist_setempty(sqlist *me)
{
	me->last = -1;
		return 0;
}
	
	int sqlist_len(sqlist *me)
{
	return (me->last+1);
}

	void sqlist_display(sqlist *me)
{
	int i;
	if(me->last == -1)
		return ;
	for(i = 0;i <= (me->last);i++)
		printf("%d ",me->data[i]);
		printf("\n");
}
	
	void sqlist_union(sqlist *me,sqlist *me1)
{
	int i;
	for(i = 0;i <= me1->last;i++)
	{
	if(sqlist_find(me,&me1->data[i]) < 0)
		sqlist_insert(me,0,&me1->data[i]);
	}

}

	int sqlist_destroy(sqlist *me)
{
	free(me);
}
